package cn.chainco.cts.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import cn.chainco.cts.iservice.CommonManager;
import cn.chainco.cts.iservice.ICompanyManager;
import cn.chainco.cts.iservice.ICurrencyManager;
import cn.chainco.cts.iservice.IDataDictionaryManager;
import cn.chainco.cts.iservice.IEmployeeManager;
import cn.chainco.cts.iservice.IFeeManager;
import cn.chainco.cts.iservice.IItemManager;
import cn.chainco.cts.jdo.Company;
import cn.chainco.cts.jdo.Currency;
import cn.chainco.cts.jdo.DataDictionary;
import cn.chainco.cts.jdo.Employee;
import cn.chainco.cts.jdo.Fee;
import cn.chainco.cts.jdo.Item;
import cn.chainco.cts.jdo.Tag;
import cn.chainco.cts.jdo.User;
import cn.chainco.cts.utils.CommUtils;
import cn.chainco.cts.utils.Constants;
import cn.chainco.cts.utils.CookieUtil;
import cn.chainco.cts.utils.MD5;
import cn.chainco.cts.utils.VarData;
import cn.chainco.cts.utils.query.OP;
import cn.chainco.cts.utils.query.Page;
import cn.chainco.cts.utils.query.QueryParam;

@SuppressWarnings("unused")
public class ActionUtil{
	/**
	 * 箱量类型
	 */
	public static final String[] boxTxt = new String[]{"20'GP", "40'GP", "40'HQ", "20'OT", "20'RF", "20'FR", "20'GM", "20'TK", "40'OT", "40'RF", "40'FR", "40'GM", "45'GP"};

	/**
	 * 获取去单引号的箱量类型
	 */
	public static String[] getBox(){
		String[] box = new String[boxTxt.length];
		for(int i = 0; i < boxTxt.length; i++){
			box[i] = "box" + boxTxt[i].replaceAll("\'", "");
		}
		return box;
	}

	public static Map<String, String> boxMap(){
		Map<String, String> box = new HashMap<String, String>();
		for(int i = 0; i < boxTxt.length; i++){
			box.put("box" + boxTxt[i].replaceAll("'", ""), boxTxt[i]);
		}
		return box;
	}

	public static void outputData(Object obj){
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("html/text;charset=utf-8");
		response.setHeader("progma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		try{
			response.getWriter().print(obj);
			response.getWriter().flush();
			response.getWriter().close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}

	/**
	 * <pre>
	 * 根据flag获取所有的选择项集合
	 * @param compnay 公司
	 * @param moduleFlag 模块(fcl,lcl,air...)
	 * @param type 0.币种，1.委托项目,2.公司费用,3.标签,4.员工,5.数据字典,6.文档模板
	 * @param status 状态：0正常；1禁用(null为全部)
	 * @param 当查数据字典时(company 为null)
	 * @param 当查数据字典时（moduleFlag：1.FCL，2.LCL，3.AIR，4.EXPRESS，5.TRUCKING，6.OTHERS）
	 * @param 当查数据字典时（typeDetail：1.货物类型；2.交接方式；3.运费支付方式；4.放货方式；5.包装类型 6.接驳工具；7.承运人（船东/航空公司/快递公司））
	 * @param 当查委托项目(typeDetail:null|0为全部，1为系统默认，2.除系统默认,3.文件加工，货物类型)
	 * 
	 */
	@SuppressWarnings("unchecked")
	public static <E>List<E> getCompanyParam(Company company, Integer moduleFlag, Integer type, Integer typeDetail, Integer status){
		HttpServletRequest request = ServletActionContext.getRequest();
		String language = CookieUtil.getCookieValue(request, "cts_locale");
		List<E> list = null;

		if(type == 0){
			ICompanyManager companyManager = (ICompanyManager)getBeanByName("companyManager");
			ICurrencyManager currencyManager = (ICurrencyManager)getBeanByName("currencyManager");
			List<Currency> xlist = currencyManager.getAllCurrency();
			for(Currency item: xlist){
				if(!language.equals("zh_CH")){
					item.setCnName(item.getEnName());
				}
			}

			list = (List<E>)xlist;
		}else if(type == 1){
			IItemManager itemManager = (IItemManager)getBeanByName("itemManager");
			List<Item> xlist = new ArrayList<Item>();

			if(typeDetail != null && 1 == typeDetail){
				xlist = itemManager.findItem(null, "1,2,3,4,5", null, null, status!=null ? String.valueOf(status) : null);
			}else if(typeDetail != null && 2 == typeDetail){
				xlist = itemManager.findItem(company, "6,7,8", null, null, status!=null ? String.valueOf(status) : null);
			}else if(typeDetail != null && 3 == typeDetail){
				xlist = itemManager.findItem(company, "6,7", null, null, status!=null ? String.valueOf(status) : null);
			}else{
				xlist = itemManager.findItem(null, "1,2,3,4,5" , null, null, status!=null ? String.valueOf(status) : null);
				List<Item> xlist2 = itemManager.findItem(company, null, null, null, status!=null ? String.valueOf(status) : null);
				if(xlist2.size() > 0){
					xlist.addAll(xlist2);
				}
			}

			for(Item item: xlist){
				if(language.equals("zh_CH")){
					item.setName(item.getName_zh());
				}else{
					item.setName(item.getName_en());
				}
			}

			list = (List<E>)xlist;
		}else if(type == 2){
			IFeeManager feeManager = (IFeeManager)getBeanByName("feeManager");
			List<Fee> xlist = feeManager.findFeeByCompany(company, status);
			for(Fee item: xlist){
				if(language.equals("zh_CH")){
					item.setName(item.getName_zh());
				}else{
					item.setName(item.getName_en());
				}
			}

			list = (List<E>)xlist;
		}else if(type == 3){
			ICompanyManager companyManager = (ICompanyManager)getBeanByName("companyManager");
			List<Tag> xlist = companyManager.findTagByCompany(company, status);
			list = (List<E>)xlist;
		}else if(type == 4){
			IEmployeeManager employeeManager = (IEmployeeManager)getBeanByName("employeeManager");
			List<Employee> xlist = employeeManager.findEmployeeByCompany(company);
			list = (List<E>)xlist;
		}else if(type == 5){
			IDataDictionaryManager dataDictionaryManager = (IDataDictionaryManager)getBeanByName("dataDictionaryManager");
			if(moduleFlag > 5){
				moduleFlag = 6;
			}
			List<DataDictionary> xlist = dataDictionaryManager.findDataByType(moduleFlag, typeDetail, status, null);
			for(DataDictionary item: xlist){
				if(language.equals("zh_CH")){
					item.setName(item.getName_zh());
				}else{
					item.setName(item.getName_en());
				}
			}
			list = (List<E>)xlist;
		}else if(type == 6){

		}

		return list;
	}

	/**
	 * 获取接口的实现类
	 */
	@SuppressWarnings("unchecked")
	public static <E>E getBeanByName(String name){
		WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
		return (E)context.getBean(name);
	}

	public static CommonManager getCommonManager(){
		return (CommonManager)getBeanByName("commonManager");
	}

	/**
	 * 获取登录用户
	 * @return
	 */
	public static User getLoginUser(){
		String id = CookieUtil.getCookieValue(ServletActionContext.getRequest(), "cts_userid");
		if(CommUtils.isCorrectNumber(id)){
			User user = getCommonManager().findEntityByPK(User.class, Long.parseLong(CookieUtil.getCookieValue(ServletActionContext.getRequest(), "cts_userid")));
			return user;
		}else{
			return null;
		}
	}

	/**
	 * 获取登录用户公司(admin)
	 * @return
	 */
	public static Company getLoginUserCompany(){
		String id = CookieUtil.getCookieValue(ServletActionContext.getRequest(), "cts_companyid");
		if(CommUtils.isCorrectNumber(id)){
			return getCommonManager().findEntityByPK(Company.class, Long.parseLong(CookieUtil.getCookieValue(ServletActionContext.getRequest(), "cts_companyid")));
		}else{
			return null;
		}
	}

	/**
	 * 获取选中的员工
	 * @return
	 */
	public static Employee getSelectEmployee(){
		String id = CookieUtil.getCookieValue(ServletActionContext.getRequest(), "cts_employeecpyid");
		if(CommUtils.isCorrectNumber(id)){
			return getCommonManager().findEntityByPK(Employee.class, Long.parseLong(id));
		}else{
			return null;
		}
	}

	/**
	 * 获取管理员用户
	 * @return
	 */
	public static User getAdmin(){
		List<QueryParam> paramList = new ArrayList<QueryParam>();
		QueryParam param = new QueryParam();
		param.setField("userName");
		param.setOp(OP.equal);
		param.setValue(new Object[]{VarData.adminName});
		paramList.add(param);
		return getCommonManager().findByCustomized(User.class, paramList, null).get(0);
	}

	/**
	 * 获取用户在公司的员工或访客身份
	 */
	public static Employee getEmployeeByUser(User user, Company company){
		IEmployeeManager employeeManager = getBeanByName("employeeManager");
		return employeeManager.findEmployeeByUser(user, company);
	}

	/**
	 * 获取登录用户在公司的员工或访客身份
	 */
	public static Employee getEmployeeByLogin(Company company){
		IEmployeeManager employeeManager = getBeanByName("employeeManager");
		return employeeManager.findEmployeeByUser(ActionUtil.getLoginUser(), company);
	}

	/**
	 * 获取top.jsp所需的信息,保存在request
	 * 
	 */
	public static void getTopAllInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		User user = getLoginUser(); // 用户

		if(user != null && user.getStatus() == 0){
			List<Employee> emylist = new ArrayList<Employee>();
			List<Employee> _el = new ArrayList<Employee>();
			List<Employee> _el2 = new ArrayList<Employee>();

			String userName = CookieUtil.getCookieValue(request, "cts_username");
			String language = CookieUtil.getCookieValue(request, "cts_locale");
			String employeeId = CookieUtil.getCookieValue(request, "cts_employeecpyid");
			String employeeId2 = request.getParameter("selectEmpId");

			String elem1 = request.getParameter("elem1");
			String elem2 = request.getParameter("elem2");
			String elem3 = request.getParameter("elem3");

			Company company = null;
			Employee selectEmployee = null;
			Employee selectEmployee2 = null;

			if(user != null){
				ICompanyManager companyManager = (ICompanyManager)getBeanByName("companyManager");
				company = companyManager.findCompayByUser(user);
				
				Company company2 = companyManager.findCompayByUser(user);
				request.setAttribute("company2", company2);
			}
			
			try{
				if(user != null && !VarData.adminName.equals(userName)){
					IEmployeeManager employeeManager = (IEmployeeManager)getBeanByName("employeeManager");
					CommonManager commonManager = (CommonManager)getBeanByName("commonManager");;
					emylist = employeeManager.findEmployeByUser(user, "1");
                    List<Employee> __emlist = new ArrayList<Employee>();
                    
                    // 如果员工或员工公司被禁用
                    if(CommUtils.isCorrectNumber(employeeId)){
        				Employee employee = commonManager.findEntityByPK(Employee.class, Long.valueOf(employeeId));
        				Company company2 = commonManager.findEntityByPK(Company.class, employee.getCompany().getId());
        				
        				if(company2.getStatus() !=1 || employee.getStatus()!=1 || employee.getUser()==null){
        					new ActionUtil().clearEmployeeByCookie();
        				}
        			}
                    
					for(int i = 0; i < emylist.size(); i++){
						Employee employee = emylist.get(i);
						User _user = commonManager.findEntityByPK(User.class, employee.getUser().getId());
						Company _company = null;
						if(employee.getCompany() != null){
							_company = commonManager.findEntityByPK(Company.class, employee.getCompany().getId());
						}
						employee.setCompany(_company);
						employee.setUser(_user);
						
						// fileter the fial company
						if(employee.getCompany()!=null && (employee.getCompany().getStatus() == 1 ||  employee.getStatus() == 1 || (employee.getInvite() !=null && employee.getInvite() == 2))){
							__emlist.add(employee);
							
							if(employee.getCompany().getStatus() != 1 || employee.getUser()==null || (employee.getInvite() ==null || employee.getInvite() != 2)){
								__emlist.remove(employee);
							}
						}
						

					}
 
					emylist = __emlist;
					if(emylist.size() > 3 || company == null || (company!=null && company.getStatus() != 1)){
						if((company!=null && company.getStatus() != 1) || company == null){
                            // 我的公司是否被禁用
							if(company != null && company.getStatus() != 1){
								company = null;
								if(emylist.size() > 0){
									Employee _employee_ = emylist.get(0);
									if(_employee_.getType() == 2){
										emylist.remove(0); // 当公司禁用,移除管理员
										//new ActionUtil().clearCompanyByCookie();
									}
								}
								
								
							}
							
							if(CommUtils.isCorrectNumber(elem1)){
								emylist = sortEmployeeList(emylist, 0, Long.valueOf(elem1));
							}
							
							if(CommUtils.isCorrectNumber(elem2)){
								emylist = sortEmployeeList(emylist, 1, Long.valueOf(elem2));
							}
							
							if(CommUtils.isCorrectNumber(elem3)){
								emylist = sortEmployeeList(emylist, 2, Long.valueOf(elem3));
							}
							
						}else{
							if(CommUtils.isCorrectNumber(elem1)){
								emylist = sortEmployeeList(emylist, 1, Long.valueOf(elem1));
							}
							
							if(CommUtils.isCorrectNumber(elem2)){
								emylist = sortEmployeeList(emylist, 2, Long.valueOf(elem2));
							}
						}
					}

					// 我的公司是否被禁用
					boolean isadminNot = false;
					if(company != null && company.getStatus() != 1){
						company = null;
						if(emylist.size() > 0){
							Employee _employee_ = emylist.get(0);
							if(_employee_.getType() == 2){
								emylist.remove(0); // 当公司禁用,移除管理员
								isadminNot = true;
								//new ActionUtil().clearCompanyByCookie();
							}
						}
						
						
					}

					if(emylist.size() > 3){ // 前三个
						_el = emylist.subList(0, 3);
						_el2 = emylist.subList(3, emylist.size());
					}else{
						_el = emylist; // 更多
					}

					// 内容是否长度超过15byte
					int[] noteLen = new int[_el.size()];
					for(int i = 0; i < _el.size(); i++){
						Company _company_ = _el.get(i).getCompany();
						if(_company_ != null && getStrLength(_company_.getName()) > 15){
							noteLen[i] = 1;
						}else{
							noteLen[i] = 0;
						}
					}

					request.setAttribute("len", noteLen);
					if(emylist!=null && emylist.size() > 0){
						boolean isExist = false;
						for (Employee em : emylist) {
							if(employeeId!=null && !"".equals(employeeId) &&  em.getId() == Long.valueOf(employeeId)){
								isExist = true;
							}
						}
						
						if(isExist == false){
							employeeId = String.valueOf(emylist.get(0).getId());
							ActionUtil.addCompanyToCookie(emylist.get(0).getCompany(), -1);
							ActionUtil.addEmployeeToCookie(emylist.get(0), -1);
							if(isadminNot == true){
								request.setAttribute("isstop", 1);
							}
						}
						
					}
				}
				
			}catch(Exception e){
				e.printStackTrace();
			}

			request.setAttribute("language", language);
			request.setAttribute("user", user);
			request.setAttribute("emylist", _el);
			request.setAttribute("emylist2", _el2);
			request.setAttribute("employeeId", employeeId);
			request.setAttribute("company", company);
		}
	}

	/**
	 * 排位
	 */
	private static List<Employee> sortEmployeeList(List<Employee> list, int setIndex, Long id){
		for(int i = 0; i < list.size(); i++){
			Employee employee = list.get(i);
			if(employee.getId() == id && employee.getType() != 2){
				Employee selectEmployee = employee;
				list.remove(i);
				list.add(setIndex, selectEmployee);
			}
		}

		if(list != null && list.size() > 0 && list.get(0).getType() != 2){
			for(int i = 0; i < list.size(); i++){
				Employee employee = list.get(i);
				if(employee.getType() == 2){
					Employee selectEmployee = employee;
					list.remove(i);
					list.add(0, selectEmployee);
				}
			}
		}
		return list;
	}

	public void reSetPath(String fromFile, String toFile){
		try{
			String filePath = ServletActionContext.getServletContext().getRealPath(fromFile);
			String newPath = ServletActionContext.getServletContext().getRealPath(toFile);

			File file = new File(filePath);
			if(file.exists()){
				file.renameTo(new File(newPath));
				deleteFilex(file);
			}
		}catch(RuntimeException e){
			e.printStackTrace();
		}
	}

	private void deleteFilex(File file){
		if(file.getName().equals(Constants.USER_FILENAME) || file.getName().equals(Constants.COMPANY_FILENAME) || file.getName().equals(Constants.BASE_DIR)){
			return;
		}else{
			if(file.exists()){ // 判断文件是否存在
				if(file.isFile()){ // 判断是否是文件
					file.delete(); // delete()方法 你应该知道 是删除的意思;
				}else if(file.isDirectory()){ // 否则如果它是一个目录
					File files[] = file.listFiles(); // 声明目录下所有的文件 files[];
					for(int i = 0; i < files.length; i++){ // 遍历目录下所有的文件
						this.deleteFilex(files[i]); // 把每个文件 用这个方法进行迭代
					}
					
					if(!file.getName().equals(Constants.USER_FILENAME) && !file.getName().equals(Constants.COMPANY_FILENAME) && !file.getName().equals(Constants.BASE_DIR)){
						file.delete();
					}
				}
			}
		}

	}
	
	/**
	 * 删除文件
	 * @param file
	 * @param noInPath
	 */
	public static void deleteFileX1(File file){
	   	ActionUtil au = new ActionUtil();
	   	au.deleteDoc(file);
	}
	
	private void deleteDoc(File file){
		if(file.exists()){ // 判断文件是否存在
			if(file.isFile()){ // 判断是否是文件
				file.delete(); // delete()方法 你应该知道 是删除的意思;
			}else if(file.isDirectory()){ // 否则如果它是一个目录
				File files[] = file.listFiles(); // 声明目录下所有的文件 files[];
				for(int i = 0; i < files.length; i++){ // 遍历目录下所有的文件
					this.deleteDoc(files[i]); // 把每个文件 用这个方法进行迭代
				}
				
				if(!file.getName().equals(Constants.WORK_FILE) &&  !file.getName().equals(Constants.BASE_DIR) && !file.getName().equals(Constants.COMPANY_FILENAME)){
					file.delete();
				}
			}
		}

	}

	public static void addUserToCookie(User user, Integer times){
		HttpServletRequest request = ServletActionContext.getRequest();
		String strTime = CookieUtil.getCookieValue(request, "cts_times");
		if(CommUtils.isCorrectNumber(strTime) && times == null){
			times = Integer.valueOf(strTime);
		}

		CookieUtil.addCookie(ServletActionContext.getResponse(), "cts_password", MD5.toMD5(user.getUserPass()), "/", times);
		CookieUtil.addCookie(ServletActionContext.getResponse(), "cts_username", user.getUserName(), "/", times);
		CookieUtil.addCookie(ServletActionContext.getResponse(), "cts_logopath", user.getLogoPath(), "/", times);
		CookieUtil.addCookie(ServletActionContext.getResponse(), "cts_userid", String.valueOf(user.getId()), "/", times);
		CookieUtil.addCookie(ServletActionContext.getResponse(), "cts_times", String.valueOf(times), "/", times);
	}
	
	public static void addCompanyToCookie(Company company , Integer times){
		HttpServletRequest request = ServletActionContext.getRequest();
		String strTime = CookieUtil.getCookieValue(request, "cts_times");
		if(CommUtils.isCorrectNumber(strTime) && times == null){
			times = Integer.valueOf(strTime);
		}

		CookieUtil.addCookie(ServletActionContext.getResponse(), "cts_companyid", company.getId()+"" , "/", times);
		CookieUtil.addCookie(ServletActionContext.getResponse(), "cts_emplcpyid", company.getId() +"" , "/", times);
		CookieUtil.addCookie(ServletActionContext.getResponse(), "cts_companyName", company.getName() , "/", times);
		CookieUtil.addCookie(ServletActionContext.getResponse(), "cts_companyPrefix", company.getPrefix(), "/", times);
		CookieUtil.addCookie(ServletActionContext.getResponse(), "cts_companyLogoPath", company.getLogoPath(), "/", times);
		CookieUtil.addCookie(ServletActionContext.getResponse(), "cts_companyStatus", company.getStatus().toString() , "/", times);
	}
	
	public static void addEmployeeToCookie(Employee employee , Integer times){
		HttpServletRequest request = ServletActionContext.getRequest();
		String strTime = CookieUtil.getCookieValue(request, "cts_times");
		if(CommUtils.isCorrectNumber(strTime) && times == null){
			times = Integer.valueOf(strTime);
		}

		CookieUtil.addCookie(ServletActionContext.getResponse(), "cts_employeecpyid" , employee.getId()+"" , "/", times);
		CookieUtil.addCookie(ServletActionContext.getResponse(), "cts_employeetype"  , employee.getType()+"" , "/", times);
		CookieUtil.addCookie(ServletActionContext.getResponse(), "cts_emplcpytype", employee.getType() +"" , "/", times);
	}

	/**
	 * 清空Cookie
	 *
	 */
	public static void clearUserByCookie(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		request.getSession().invalidate();
		Cookie[] cookies = request.getCookies();

		if(cookies != null){
			for(int i = 0; i < cookies.length; i++){
				String name = cookies[i].getName();
				if(CommUtils.isValidStr(name) && name.startsWith("cts_")){
					cookies[i].setMaxAge(0);
					cookies[i].setPath("/");
					response.addCookie(cookies[i]);
				}
			}
		}
	}
	
	/**
	 * 清空员工Cookie
	 *
	 */
	public void clearEmployeeByCookie(){
		clearCookieByKey("cts_employeecpyid");
		clearCookieByKey("cts_employeetype");
	}
	
	/**
	 * 清空公司Cookie
	 *
	 */
	public void clearCompanyByCookie(){
		clearCookieByKey("cts_companyid");
		clearCookieByKey("cts_emplcpyid");
		clearCookieByKey("cts_companyName");
		clearCookieByKey("cts_companyPrefix");
		clearCookieByKey("cts_companyLogoPath");
		clearCookieByKey("cts_companyStatus");
	}
	
	/***
	 * 清除cookie
	 * @param key cookie名
	 * @return
	 */
	public int clearCookieByKey(String key){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		Cookie[] cookies = request.getCookies();

		int code = 0;
		if(cookies != null){
			for(int i = 0; i < cookies.length; i++){
				String name = cookies[i].getName();
				if(CommUtils.isValidStr(name) && name.startsWith(key)){
					cookies[i].setMaxAge(0);
					cookies[i].setPath("/");
					response.addCookie(cookies[i]);
				    code = 1;
				}
			}
		}else{
			code = 1;
		}
		
		return code;
	}
	
	@SuppressWarnings("unchecked")
	public static <E>List<E> findListByE(Class className, String sql){
		List<QueryParam> paramList = new ArrayList<QueryParam>();
		QueryParam param = new QueryParam();
		param.setField(sql);
		param.setOp(OP.sql);
		param.setValue(new Object[]{});
		paramList.add(param);

		CommonManager commonManager = (CommonManager)getBeanByName("commonManager");
		List<E> elist = commonManager.findByCustomized(className, paramList, null);
		return elist;
	}

	public static HttpSession session;

	public static HttpSession getSession(){
		if(session == null){
			session = ServletActionContext.getRequest().getSession();
		}

		return session;
	}

	/**
	 * 得到字符串的长度
	 */
	public static int getStrLength(String str){
		if(str == null || str.length() <= 0){
			return 0;
		}

		int len = 0;
		char c;
		for(int i = str.length() - 1; i >= 0; i--){
			c = str.charAt(i);
			if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')){
				// 字母, 数字
				len++;
			}else{
				if(Character.isLetter(c)){ // 中文
					len += 2;
				}else{ // 符号或控制字符
					len++;
				}
			}
		}

		return len;
	}


}
